package oboe.controllers.jml.helpers;

import flash.display.GradientType;
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.geom.Matrix;
import flash.geom.Rectangle;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFieldType;
import flash.text.TextFormat;
import flash.text.TextLineMetrics;

import oboe.controllers.jml.events.JMLEvent;
import oboe.core.Controller;

class JMLButton extends Sprite
{
private var label:TextField;
public var buttonColor1:Int = 0xC0C0C0;
public var buttonColor2:Int = 0x909090;
public var buttonBorderColor:Int = 0x707070;
public var textColor:Int = 0xF0F0F0;
public var textSize:Int = 16;
public var textFont:String = null;

public var text:String;

public static inline var SIG_ONCLICKname:String = "onClick****";

public function new()
{
	super();
	this.addEventListener( JMLEvent.JMLCOMPLETE, onComplete);
	this.label = new TextField();
	this.addChild( this.label );
	Object(this).addCallback( MouseEvent.CLICK, onClick );
}

private function onClick():Void
{
	var pc:Controller = Object(this.parent).getController();
	if( !pc ) return;
	pc.message("onClick" + this.name);
}

private function onComplete( e:JMLEvent ):Void
{
	var box:Matrix = new Matrix();
	var width:Float = e.source.width;
	var height:Float = e.source.height;
	this.scaleX = this.scaleY = 1;
	box.createGradientBox( width, height, Math.PI/2 );
	this.graphics.lineStyle(2, this.buttonBorderColor, 1);
	this.graphics.beginGradientFill( GradientType.LINEAR, [this.buttonColor1, this.buttonColor2], [1, 1], [0, 255], box );
	this.graphics.drawRect(0, 0, width, height);
	this.graphics.endFill();

	var tf:TextFormat = new TextFormat( this.textFont, this.textSize, this.textColor );
	this.label.defaultTextFormat = tf;
	this.label.autoSize = TextFieldAutoSize.LEFT;
	this.label.text = this.text;
	this.label.selectable = false;
	this.label.x = width/2 - this.label.width/2;
	this.label.y = height/2 - this.label.height/2;
	
	this.scrollRect = new Rectangle(0, 0, width, height );
	this.mouseChildren = false;
	this.buttonMode = true;
}

}